﻿using Aurora.BusinessLogic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Aurora.Web.Mantenimiento
{
    public partial class frmMantenimientoCliente : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                OcultarMensajes();
                gridClientes.DataSource = (new BL_Cliente()).MAN_Cliente_Listar(0, string.Empty, string.Empty, string.Empty);
                gridClientes.DataBind();
            }
        }

        protected void btnBuscar_Click(object sender, EventArgs e)
        {
            OcultarMensajes();
            hfNombre.Value = inNombre.Value;
            hfRuc.Value = inRUC.Value;
            BindData();
        }

        protected void gridClientes_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Update"))
            {
                GridViewRow oRow = gridClientes.Rows[Convert.ToInt32(e.CommandArgument)];
                Finalizado((new BL_Cliente()).MAN_Cliente_Insertar(
                                                            Convert.ToInt32((oRow.FindControl("lblID") as Label).Text),
                                                            (oRow.FindControl("txtNombre") as TextBox).Text,
                                                            (oRow.FindControl("txtRUC") as TextBox).Text,
                                                            (oRow.FindControl("txtDireccion") as TextBox).Text));
            }
            else if (e.CommandName.Equals("Delete"))
            {
                GridViewRow oRow = gridClientes.Rows[Convert.ToInt32(e.CommandArgument)];
                Finalizado((new BL_Cliente()).MAN_Cliente_Insertar(
                                                            Convert.ToInt32((oRow.FindControl("lblID") as Label).Text),
                                                            (oRow.FindControl("lblNombre") as Label).Text,
                                                            (oRow.FindControl("lblRUC") as Label).Text,
                                                            (oRow.FindControl("lblDireccion") as Label).Text,
                                                            false));
            }
        }

        protected void gridClientes_RowEditing(object sender, GridViewEditEventArgs e)
        {
            OcultarMensajes();
            gridClientes.EditIndex = e.NewEditIndex;
            BindData();
        }

        protected void gridClientes_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gridClientes.EditIndex = -1;
            BindData();
            OcultarMensajes();
        }

        protected void gridClientes_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            gridClientes.EditIndex = -1;
            BindData();
        }

        private void BindData()
        {
            gridClientes.DataSource = (new BL_Cliente()).MAN_Cliente_Listar(0, hfNombre.Value, hfRuc.Value, string.Empty);
            gridClientes.DataBind();
        }

        protected void gridClientes_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridClientes.PageIndex = e.NewPageIndex;
            BindData();
        }

        private void Finalizado(bool Exito)
        {
            divSuccess.Visible = Exito;
            divError.Visible = !Exito;
        }
        private void OcultarMensajes()
        {
            divSuccess.Visible = divError.Visible = false;
        }

        protected void gridClientes_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            BindData();
        }

        protected void btnAgregarNuevo_Click(object sender, EventArgs e)
        {
            Finalizado((new BL_Cliente()).MAN_Cliente_Insertar(0,
                                                            txtNuevoNombre.Value,
                                                            txtNuevoRUC.Value,
                                                            txtNuevaDireccion.Value));
            BindData();
        }
    }
}